import { queryLogisticsRule, queryLogisticsByCode, getTracking } from '@/services/myOrder/trail';

export default {
    namespace: 'trail',

    state: {
        orderResult: {
            code: 0,
            messageId: '',
            messageStr: '',
            result: [],
        },
        data: [],
        trackData: {},
        formValues: {},
        orderValues: {},
    },

    effects: {
        *fetch({ payload }, { call, put }) {
            const response = yield call(queryLogisticsRule, payload);
            yield put({
                type: 'save',
                payload: response,
            });
        },
        *queryCode({ payload }, { call, put }) {
            const orderResult = yield call(queryLogisticsByCode, payload);
            yield put({
                type: 'save',
                payload: {
                    orderResult: orderResult,
                },
            });
        },
        *setData({ payload }, { call, put }) {
            yield put({
                type: 'save',
                payload: [],
            });
        },
        *getTracking({ payload }, { call, put }) {
            const response = yield call(getTracking, payload);
            const { data, code } = response;
            console.log('data:================', data)
            yield put({
                type: 'saveAllValus',
                payload: {
                    trackData: data,
                },
            });
        },
        //设置表单值：
        *allValus({ payload }, { _, put }) {
            yield put({
                type: 'saveAllValus',
                payload,
            });
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        saveAllValus(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        list(state, { payload }) {
            let param = '';
            for (let k in payload) {
                param = k;
            }
            return {
                ...state,
                [param]: { ...payload[param] },
            };
        },
    },
};
